package com.dure.hotmemory.controllers;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;


import com.dure.commonlib.util.AssetsUtil;
import com.dure.commonlib.util.PreferencesUtils;
import com.dure.commonlib.util.StreamUtil;
import com.dure.commonlib.util.ToastUtils;

import com.dure.fqllib.Merchant;
import com.dure.fqllib.OrderUtils;
import com.dure.hotmemory.BuildConfig;
import com.dure.hotmemory.bean.Planet;
import com.dure.hotmemory.config.Config;
import com.dure.hotmemory.scenes.PayScene;
import com.fuqianla.paysdk.FuQianLa;
import com.fuqianla.paysdk.FuQianLaPay;
import com.google.gson.Gson;

import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by dure on 2017/4/17.
 */

public class PayController extends Controller {
    public Handler handler = new Handler(activity.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    activity.onBackPressed();
                    break;
                case 1:
                    ToastUtils.show(baseSurfaceView.getContext(), "该渠道正在准备中， 请选择其它渠道");
                    break;
            }
        }
    };

    private PayScene payScene;

    public PayController(PayScene baseScene) {
        super(baseScene);
        payScene = baseScene;
    }

    public Planet getAllPlanets() {
        InputStream is = AssetsUtil.getAssets(baseSurfaceView.getContext(), "pay_planets.json");
        Gson gson = new Gson();
        String universeJsonStr = StreamUtil.inputStream2String(is);
        Planet universe = gson.fromJson(universeJsonStr, Planet.class);

        return universe;
    }

    public void goToPay(Planet planet, String payType, double amount) {

        if (BuildConfig.DEBUG)
            amount = 0.01;

        if (!payScene.isPayStarted()) {
            payScene.setPayStarted(true);
            FuQianLaPay pay = new FuQianLaPay.Builder(activity)
                    .model(FuQianLa.MODEL_PRE_CHANNEL)//通道前置模式
                    .orderID(OrderUtils.getOutTradeNo())//订单号
                    .md5PrivateKey(Merchant.MD5_PRIVATE_KEY)
                    .amount(amount)//金额
                    .subject("热记忆")//商品名称
                    .body(payType)//商品交易详情
                    .notifyUrl(Merchant.MERCHANT_NOTIFY_URL)
                    .build();

            if (TextUtils.equals(planet.clazz, "alipay")) {
                pay.startPay(FuQianLa.ALI);
            } else if (TextUtils.equals(planet.clazz, "wx")) {
                pay.startPay(FuQianLa.WX);
            }
        }
    }

    public void addPayUser(double amount) {
        // 发送登录成功请求并返回支付状态,
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));
        paramMap.put("pay_state", "1");
        paramMap.put("pay_time", String.valueOf(System.currentTimeMillis() / 1000));
        if (amount == 60) {
            PreferencesUtils.putBoolean(activity, "move_ad", true);
            paramMap.put("ad_state", "1");
        } else {
            paramMap.put("ad_state", "0");
        }

//        RequestModel requestModel = new RequestModel();
        requestModel.doPost(Config.HOST + Config.ADD_PAY_USER, paramMap);
    }


    // 将打赏用户上传保存服务器
    private void addRewardUser(double amount) {
        HashMap<String, String> paramMap = new HashMap<>();
        if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "phone")))
            paramMap.put("phone", PreferencesUtils.getString(activity, "phone"));
        else if (!TextUtils.isEmpty(PreferencesUtils.getString(activity, "uid")))
            paramMap.put("uid", PreferencesUtils.getString(activity, "uid"));
        paramMap.put("amount", String.valueOf(amount));

//        RequestModel requestModel = new RequestModel();
        requestModel.doPost(Config.HOST + Config.ADD_REWARD_USER, paramMap);
    }


}
